﻿using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Utilities.Encoders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Mbs.Utils.GM
{
    public class CryptogramUtil
    {
        private readonly string PublicKey ;
        private readonly string PrivateKey;

        private  readonly string SM4_key;
        private  readonly string SM4_iv ;


        /// <summary>
        /// 
        /// </summary>
        /// <param name="SM2PublicKey">可使用GenerateKeyPair 创建</param>
        /// <param name="SM2PrivateKey">可使用GenerateKeyPair 创建</param>
        /// <param name="SM4_Key">32位</param>
        /// <param name="SM4_IV">32位</param>
        public CryptogramUtil(string SM2PublicKey="",string SM2PrivateKey="",string SM4_Key="", string SM4_IV="")
        {
            PublicKey = SM2PublicKey;
            PrivateKey = SM2PrivateKey;
            SM4_key = SM4_Key;
            SM4_iv = SM4_IV;
        }



        public (string publicKey, string privateKey) GenerateKeyPair()
        {
            var kp = GM.GenerateKeyPair();
            var privateKey = Hex.ToHexString(((ECPrivateKeyParameters)kp.Private).D.ToByteArray()).ToUpper();
            var publicKey = Hex.ToHexString(((ECPublicKeyParameters)kp.Public).Q.GetEncoded()).ToUpper();

            return (publicKey, privateKey);
        }

     

        /// <summary>
        /// SM2加密
        /// </summary>
        /// <param name="plainText"></param>
        /// <returns></returns>
        public  string SM2Encrypt(string plainText)
        {
            return GMUtil.SM2Encrypt(PublicKey, plainText);
        }

        /// <summary>
        /// SM2解密
        /// </summary>
        /// <param name="cipherText"></param>
        /// <returns></returns>
        public  string SM2Decrypt(string cipherText)
        {
            return GMUtil.SM2Decrypt(PrivateKey, cipherText);
        }

        /// <summary>
        /// SM4加密（ECB）
        /// </summary>
        /// <param name="plainText"></param>
        /// <returns></returns>
        public  string SM4EncryptECB(string plainText)
        {
            return GMUtil.SM4EncryptECB(SM4_key, plainText);
        }

        /// <summary>
        /// SM4解密（ECB）
        /// </summary>
        /// <param name="cipherText"></param>
        /// <returns></returns>
        public  string SM4DecryptECB(string cipherText)
        {
            return GMUtil.SM4DecryptECB(SM4_key, cipherText);
        }

        /// <summary>
        /// SM4加密（CBC）
        /// </summary>
        /// <param name="plainText"></param>
        /// <returns></returns>
        public  string SM4EncryptCBC(string plainText)
        {
            return GMUtil.SM4EncryptCBC(SM4_key, SM4_iv, plainText);
        }

        /// <summary>
        /// SM4解密（CBC）
        /// </summary>
        /// <param name="cipherText"></param>
        /// <returns></returns>
        public  string SM4DecryptCBC(string cipherText)
        {
            return GMUtil.SM4DecryptCBC(SM4_key, SM4_iv, cipherText);
        }
    }
}
